<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="序号" prop="序号">
        <el-input
          v-model="queryParams.序号"
          placeholder="请输入序号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="牛场编号" prop="牛场编号">
        <el-input
          v-model="queryParams.牛场编号"
          placeholder="请输入牛场编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="管理号" prop="管理号">
        <el-input
          v-model="queryParams.管理号"
          placeholder="请输入管理号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:mndhi:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:mndhi:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:mndhi:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:mndhi:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mndhiList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="序号" />
      <el-table-column label="牛场编号" align="center" prop="牛场编号" />
      <el-table-column label="管理号" align="center" prop="管理号" />
      <el-table-column label="胎次" align="center" prop="胎次" />
      <el-table-column label="采样日期" align="center" prop="采样日期" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.采样日期, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="产犊日期" align="center" prop="产犊日期" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.产犊日期, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="产犊间隔" align="center" prop="产犊间隔" />
      <el-table-column label="泌乳天数" align="center" prop="泌乳天数" />
      <el-table-column label="分组号" align="center" prop="分组号" />
      <el-table-column label="产奶量" align="center" prop="产奶量" />
      <el-table-column label="乳脂率" align="center" prop="乳脂率" />
      <el-table-column label="蛋白率" align="center" prop="蛋白率" />
      <el-table-column label="脂蛋比" align="center" prop="脂蛋比" />
      <el-table-column label="体细胞数" align="center" prop="体细胞数" />
      <el-table-column label="体细胞分" align="center" prop="体细胞分" />
      <el-table-column label="尿素氮" align="center" prop="尿素氮" />
      <el-table-column label="乳糖率" align="center" prop="乳糖率" />
      <el-table-column label="奶损失" align="center" prop="奶损失" />
      <el-table-column label="奶款差" align="center" prop="奶款差" />
      <el-table-column label="经济损失" align="center" prop="经济损失" />
      <el-table-column label="校正奶" align="center" prop="校正奶" />
      <el-table-column label=" 持续力" align="center" prop="持续力" />
      <el-table-column label="WHI" align="center" prop="WHI" />
      <el-table-column label="前奶量" align="center" prop="前奶量" />
      <el-table-column label="前体细胞" align="center" prop="前体细胞" />
      <el-table-column label="前体细胞分" align="center" prop="前体细胞分" />
      <el-table-column label="前奶损失" align="center" prop="前奶损失" />
      <el-table-column label="高峰奶" align="center" prop="高峰奶" />
      <el-table-column label="高峰日" align="center" prop="高峰日" />
      <el-table-column label="奶量305" align="center" prop="奶量305" />
      <el-table-column label="总奶量" align="center" prop="总奶量" />
      <el-table-column label="总乳脂" align="center" prop="总乳脂" />
      <el-table-column label="总蛋白" align="center" prop="总蛋白" />
      <el-table-column label="成年当量" align="center" prop="成年当量" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:mndhi:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:mndhi:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改母牛dhi对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="牛场编号" prop="牛场编号">
          <el-input v-model="form.牛场编号" placeholder="请输入牛场编号" />
        </el-form-item>
        <el-form-item label="管理号" prop="管理号">
          <el-input v-model="form.管理号" placeholder="请输入管理号" />
        </el-form-item>
        <el-form-item label="胎次" prop="胎次">
          <el-input v-model="form.胎次" placeholder="请输入胎次" />
        </el-form-item>
        <el-form-item label="采样日期" prop="采样日期">
          <el-date-picker clearable
            v-model="form.采样日期"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择采样日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="产犊日期" prop="产犊日期">
          <el-date-picker clearable
            v-model="form.产犊日期"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择产犊日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="产犊间隔" prop="产犊间隔">
          <el-input v-model="form.产犊间隔" placeholder="请输入产犊间隔" />
        </el-form-item>
        <el-form-item label="泌乳天数" prop="泌乳天数">
          <el-input v-model="form.泌乳天数" placeholder="请输入泌乳天数" />
        </el-form-item>
        <el-form-item label="分组号" prop="分组号">
          <el-input v-model="form.分组号" placeholder="请输入分组号" />
        </el-form-item>
        <el-form-item label="产奶量" prop="产奶量">
          <el-input v-model="form.产奶量" placeholder="请输入产奶量" />
        </el-form-item>
        <el-form-item label="乳脂率" prop="乳脂率">
          <el-input v-model="form.乳脂率" placeholder="请输入乳脂率" />
        </el-form-item>
        <el-form-item label="蛋白率" prop="蛋白率">
          <el-input v-model="form.蛋白率" placeholder="请输入蛋白率" />
        </el-form-item>
        <el-form-item label="脂蛋比" prop="脂蛋比">
          <el-input v-model="form.脂蛋比" placeholder="请输入脂蛋比" />
        </el-form-item>
        <el-form-item label="体细胞数" prop="体细胞数">
          <el-input v-model="form.体细胞数" placeholder="请输入体细胞数" />
        </el-form-item>
        <el-form-item label="体细胞分" prop="体细胞分">
          <el-input v-model="form.体细胞分" placeholder="请输入体细胞分" />
        </el-form-item>
        <el-form-item label="尿素氮" prop="尿素氮">
          <el-input v-model="form.尿素氮" placeholder="请输入尿素氮" />
        </el-form-item>
        <el-form-item label="乳糖率" prop="乳糖率">
          <el-input v-model="form.乳糖率" placeholder="请输入乳糖率" />
        </el-form-item>
        <el-form-item label="奶损失" prop="奶损失">
          <el-input v-model="form.奶损失" placeholder="请输入奶损失" />
        </el-form-item>
        <el-form-item label="奶款差" prop="奶款差">
          <el-input v-model="form.奶款差" placeholder="请输入奶款差" />
        </el-form-item>
        <el-form-item label="经济损失" prop="经济损失">
          <el-input v-model="form.经济损失" placeholder="请输入经济损失" />
        </el-form-item>
        <el-form-item label="校正奶" prop="校正奶">
          <el-input v-model="form.校正奶" placeholder="请输入校正奶" />
        </el-form-item>
        <el-form-item label=" 持续力" prop="持续力">
          <el-input v-model="form.持续力" placeholder="请输入 持续力" />
        </el-form-item>
        <el-form-item label="WHI" prop="WHI">
          <el-input v-model="form.WHI" placeholder="请输入WHI" />
        </el-form-item>
        <el-form-item label="前奶量" prop="前奶量">
          <el-input v-model="form.前奶量" placeholder="请输入前奶量" />
        </el-form-item>
        <el-form-item label="前体细胞" prop="前体细胞">
          <el-input v-model="form.前体细胞" placeholder="请输入前体细胞" />
        </el-form-item>
        <el-form-item label="前体细胞分" prop="前体细胞分">
          <el-input v-model="form.前体细胞分" placeholder="请输入前体细胞分" />
        </el-form-item>
        <el-form-item label="前奶损失" prop="前奶损失">
          <el-input v-model="form.前奶损失" placeholder="请输入前奶损失" />
        </el-form-item>
        <el-form-item label="高峰奶" prop="高峰奶">
          <el-input v-model="form.高峰奶" placeholder="请输入高峰奶" />
        </el-form-item>
        <el-form-item label="高峰日" prop="高峰日">
          <el-input v-model="form.高峰日" placeholder="请输入高峰日" />
        </el-form-item>
        <el-form-item label="奶量305" prop="奶量305">
          <el-input v-model="form.奶量305" placeholder="请输入奶量305" />
        </el-form-item>
        <el-form-item label="总奶量" prop="总奶量">
          <el-input v-model="form.总奶量" placeholder="请输入总奶量" />
        </el-form-item>
        <el-form-item label="总乳脂" prop="总乳脂">
          <el-input v-model="form.总乳脂" placeholder="请输入总乳脂" />
        </el-form-item>
        <el-form-item label="总蛋白" prop="总蛋白">
          <el-input v-model="form.总蛋白" placeholder="请输入总蛋白" />
        </el-form-item>
        <el-form-item label="成年当量" prop="成年当量">
          <el-input v-model="form.成年当量" placeholder="请输入成年当量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMndhi, getMndhi, delMndhi, addMndhi, updateMndhi } from "@/api/system/mndhi";

export default {
  name: "Mndhi",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 母牛dhi表格数据
      mndhiList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        序号: null,
        牛场编号: null,
        管理号: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询母牛dhi列表 */
    getList() {
      this.loading = true;
      listMndhi(this.queryParams).then(response => {
        this.mndhiList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        序号: null,
        牛场编号: null,
        管理号: null,
        胎次: null,
        采样日期: null,
        产犊日期: null,
        产犊间隔: null,
        泌乳天数: null,
        分组号: null,
        产奶量: null,
        乳脂率: null,
        蛋白率: null,
        脂蛋比: null,
        体细胞数: null,
        体细胞分: null,
        尿素氮: null,
        乳糖率: null,
        奶损失: null,
        奶款差: null,
        经济损失: null,
        校正奶: null,
        持续力: null,
        WHI: null,
        前奶量: null,
        前体细胞: null,
        前体细胞分: null,
        前奶损失: null,
        高峰奶: null,
        高峰日: null,
        奶量305: null,
        总奶量: null,
        总乳脂: null,
        总蛋白: null,
        成年当量: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.序号)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加母牛dhi";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const 序号 = row.序号 || this.ids
      getMndhi(序号).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改母牛dhi";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.序号 != null) {
            updateMndhi(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMndhi(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const 序号s = row.序号 || this.ids;
      this.$modal.confirm('是否确认删除母牛dhi编号为"' + 序号s + '"的数据项？').then(function() {
        return delMndhi(序号s);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/mndhi/export', {
        ...this.queryParams
      }, `mndhi_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
